package com.gfm.story.service.story;

import com.gfm.story.dao.CategoryDao;
import com.gfm.story.entity.story.Book;
import com.gfm.story.entity.story.Category;
import com.gfm.story.entity.story.Chapter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.service.story
 * @Author: Administrator
 * @CreateTime: 2019-06-03 16:15
 * @Description: ${Description}
 */
@Service
public class CategoryService {
    @Resource
    private CategoryDao categoryDao;


    public List<Category> getCategory() {
        Category category = new Category();
        List<Category> cate = category.selectAll();
        if (!cate.isEmpty()){
            return cate;
        }
        return null;
    }

    //根据分类id查找小说列表
    public Map<String,Object> getBookByCategoryId(Map<String, Object> map,Integer page) {
        Map<String,Object> map1 = new HashMap<String, Object>();
        List<Book> list = new ArrayList<Book>();
        int count = 0;
        Category category1 = new Category();
        if(map.get("CategoryId")!=null){
            list = categoryDao.getBookPageByCategoryId(map);//分页数据
            count = categoryDao.getBookCountByCategoryId(map);//总记录数
            Category category = new Category();
            category.setId(Long.parseLong(map.get("CategoryId").toString()));
            category1 = categoryDao.selectOne(category);
        }
        map1.put("category",category1);
        List<Category> listgory = this.getCategory();
        map1.put("categort", listgory);
        map1.put("total", (count+20-1)/20);
        map1.put("currentPage", page);
        map1.put("objs", list);
        return map1;
    };
}
